<template>
  <el-dialog :title="`${form.chkitemId ? '修改' : '添加'}检查项`" :visible.sync="show" :close-on-click-modal="false" :show-close="!btnLoading" width="500px" @closed="$refs.form.resetFields()">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="90px">
      <el-form-item prop="versionId" class="hide" />
      <el-form-item prop="versionCode" class="hide" />
      <el-form-item prop="chkitemId" class="hide" />
      <el-form-item prop="parentId" label="上级">
        <treeselect v-model="form.parentId" :options="treeOptions" :normalizer="normalizer" show-count placeholder="选择上级" />
      </el-form-item>
      <el-form-item label="检查项编码" prop="chkitemCode"><el-input v-model.trim="form.chkitemCode" placeholder="请输入检查项编码" :maxlength="100" /></el-form-item>
      <el-form-item label="检查项名称" prop="chkitemName"><el-input v-model.trim="form.chkitemName" placeholder="请输入检查项名称" :maxlength="200" /></el-form-item>
      <el-form-item label="备注" prop="remark"><el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :maxlength="255" /></el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否启用" prop="isEnable"><el-switch v-model="form.isEnable" :active-value="1" :inactive-value="0" /></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否删除" prop="isDelete"><el-switch v-model="form.isDelete" :active-value="1" :inactive-value="0" /></el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="btnSubmit">确 定</el-button>
      <el-button :disabled="btnLoading" @click="show = false">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
// 添加或修改检查项
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { nameRule } from '@/utils/validate'
import { editFormMixin } from '@/utils/mixin'
import { addChkitem, updateChkitem } from '@/api/checkScore/cqEval'
export default {
  components: { Treeselect },
  mixins: [editFormMixin],
  data() {
    return {
      primaryKey: 'chkitemId',
      addApi: addChkitem,
      updateApi: updateChkitem,
      form: {
        versionId: '',
        versionCode: '',
        chkitemId: '',
        parentId: '',
        chkitemCode: '',
        chkitemName: '',
        remark: '',
        isEnable: 1,
        isDelete: 0
      },
      rules: {
        chkitemCode: [{ required: true, message: '请输入检查项编码' }, nameRule],
        chkitemName: [{ required: true, message: '请输入检查项名称' }]
      },
      treeOptions: []
    }
  },
  methods: {
    opened(item, { treeData }) {
      this.treeOptions = [{ chkitemId: '', chkitemName: '主类目', children: treeData }]

      // 编辑时字段有差异
      if (!item.chkitemId) return
      this.form.versionId = item.itemVersionId
      this.form.versionCode = item.itemVersionCode
      this.form.isEnable = item.itemIsEnable
      this.form.isDelete = item.itemIsDelete
    },
    normalizer(node) {
      if (!node.children?.length) delete node.children
      return { id: node.chkitemId, label: node.chkitemName, children: node.children }
    }
  }
}
</script>
